package com.tk.exam.web.restful;

import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.regexp.internal.RE;
import com.tk.exam.domain.TkExamInfo;
import com.tk.exam.domain.TkExamNotice;
import com.tk.exam.domain.TkFile;
import com.tk.exam.domain.TkToken;
import com.tk.exam.http.Response;
import com.tk.exam.interceptor.NoCheckHttpHeader;
import com.tk.exam.service.TkStudentServer;
import com.tk.exam.service.TkTokenService;
import com.tk.exam.utils.PageInfo;
import com.tk.exam.utils.SSOUtil;
import com.tk.exam.vo.question.req.TkQuestion4CRequestVo;
import com.tk.exam.vo.question.req.TkQuestionDXRequestVo;
import com.tk.exam.vo.question.req.TkQuestionPDRequestVo;
import com.tk.exam.web.admin.UploadController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;


@RestController
@RequestMapping("tk/exam/restful")
@Slf4j
public class RestFulController {

    @Autowired
    TkTokenService tkTokenService;

    @Autowired
    TkStudentServer tkStudentServer;

    @Autowired
    UploadController uploadController;


    @GetMapping("get")
    public Response get(@RequestParam(defaultValue = "1") Integer curPage,
                        @RequestParam(defaultValue = "10") Integer pageSize) {
        return Response.succeed().put("OK", "OK");
    }


    @PostMapping("post")
    public Response post(@RequestHeader(value = "token") String token, @RequestBody Map param) {
        if (param == null) {
            return Response.fail("XXXXXX");
        }
        Map result = new HashMap();
        return Response.succeed(result);
    }

    /**
     * 分页查询以WithPage结尾
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/searchListByPage", method = RequestMethod.POST)
    public Response templateGroupListWithPage(@RequestBody JSONObject param) {
        //查询分页数据
        com.github.pagehelper.PageInfo<TkToken> pageInfoHelper = tkTokenService.searchList(param);
        PageInfo pageInfo = new PageInfo(pageInfoHelper);
        return Response.succeed(pageInfo);
    }


    /**
     * 获取考试须知内容和倒计时时间
     * @return
     */
    @NoCheckHttpHeader
    @RequestMapping(value = "/searchExamNotice", method = RequestMethod.GET)
    public Response searchExamNotice() {
        TkExamNotice examNotice = tkStudentServer.searchExamNotice();
        return Response.succeed(examNotice);
    }

    /**
     * 根据考试id查询考试概述
     * @param examId
     * @return
     */
    @NoCheckHttpHeader
    @RequestMapping(value = "/searchExamInfo", method = RequestMethod.GET)
    public Response searchExamInfo(@RequestHeader("examId") Integer examId) {
        TkExamInfo examInfo = tkStudentServer.searchExamInfo(examId);
        return Response.succeed(examInfo);
    }

    /**
     * 开始考试
     * @param examId
     * @return
     */
    @RequestMapping(value = "/startExam", method = RequestMethod.GET)
    public Response startExam(@RequestHeader("examId") Integer examId) {
        tkStudentServer.startExam(examId);
        return Response.succeed();
    }

    /**
     * 获取试题内容
     * @param examId
     * @return
     */
    @RequestMapping(value = "/searchExamPaperContent", method = RequestMethod.GET)
    public Response searchExamPaperContent(@RequestHeader("examId") Integer examId) {
        Map examPaperContent = tkStudentServer.searchExamPaperContent(examId);
        return Response.succeed(examPaperContent);
    }


    /**
     * 提交判断题答案
     * @param examId
     * @param object
     * @return
     */
    @RequestMapping(value = "/submitJudgeAnswer", method = RequestMethod.POST)
    public Response submitJudgeAnswer(@RequestHeader("examId") Integer examId,
                                      @RequestBody TkQuestionPDRequestVo object) {
        tkStudentServer.submitJudgeAnswer(examId,object);
        return Response.succeed();
    }

    /**
     * 提交单选题答案
     * @param object
     * @return
     */
    @RequestMapping(value = "/submitRadioAnswer", method = RequestMethod.POST)
    public Response submitRadioAnswer(@RequestHeader("examId") Integer examId,
                                      @RequestBody TkQuestionDXRequestVo object) {
        tkStudentServer.submitRadioAnswer(examId,object);
        return Response.succeed();
    }

    /**
     * 提交4C题答案
     * @param object
     * @return
     */
    @RequestMapping(value = "/submitFourCAnswer", method = RequestMethod.POST)
    public Response submitFourCAnswer(@RequestHeader("examId") Integer examId,
                                      @RequestBody TkQuestion4CRequestVo object) {
        tkStudentServer.submitFourCAnswer(examId,object);
        return Response.succeed();
    }


    /**
     * 提交试卷
     * @param examId
     * @return
     */
    @RequestMapping(value = "/commitExamPaper", method = RequestMethod.GET)
    public Response commitExamPaper(@RequestHeader("examId") Integer examId,
                                    Boolean commitFlag){
        tkStudentServer.commitExamPaper(examId,commitFlag);
        return searchExamCompleteInfo(examId);
    }

    /**
     * 查询试卷完成情况
     * @param examId
     * @return
     */
    @RequestMapping(value = "/searchExamCompleteInfo", method = RequestMethod.GET)
    public Response searchExamCompleteInfo(@RequestHeader("examId") Integer examId){
        Map<String,Object> resultMap = tkStudentServer.searchExamCompleteInfo(examId);
        return Response.succeed().put("completeInfo",resultMap);
    }


    /**
     * 重新计算考生分数
     * @param examId
     * @return
     */
    @RequestMapping(value = "/repeatCalculate", method = RequestMethod.GET)
    public Response repeatCalculate(Integer examId,Integer studentId){
        tkStudentServer.repeatCalculate(examId,studentId);
        return Response.succeed();
    }


    /**
     * 上传图片
     * @param entityId
     * @param files
     * @return
     */
    @PostMapping("/upload")
    public Response upload(@RequestParam(value = "entityId", required = false) String entityId, @RequestParam("files") MultipartFile[] files) {
        String entityType = "question";
        return uploadController.upload(entityType,entityId,files);
    }


    /**
     * 获取考生考试剩余时间
     * @param examId
     * @return
     */
    @RequestMapping(value = "/searchRemainTime", method = RequestMethod.GET)
    public Response searchRemainTime(@RequestHeader("examId") Integer examId){
        Map<String,Object> remainTime = tkStudentServer.searchRemainTime(examId);
        return Response.succeed().put("remainTime",remainTime);
    }

}
